(function() {
    'use strict';
    angular
    	.module('app')
    	.controller('logCtrl', logCtrl);
    	logCtrl.$inject = ['$scope','config','http','myUtils'];
      	function logCtrl($scope,config,http,myUtils) {
      		var vm = $scope.vm = {};
      		angular.extend( vm,{
      			init : function(){
      				vm.getLogs();
      				vm.pageCount = config.page;
      			},
      			page :{
      				size : 10,
      				offset : 0,
      				cur : 1
      			},
      			listPageCount : function( pc ){
      				vm.page.size = pc;
      				vm.page.cur = 1;
      				vm.getLogs();
      			},
      			goNext : function(){
      				vm.page.offset = vm.page.offset+vm.page.size;
      				vm.page.cur = vm.page.cur + 1;
      				vm.getLogs();
      			},
      			goPrevious : function(){
      				if( vm.page.offset > 0){
      					vm.page.cur = vm.page.cur - 1;
      					vm.page.offset = vm.page.offset-vm.page.size;
      					vm.getLogs();
      				}
      			},
      			getLogs : function(){
      				/***获取动态信息***/
      				vm.log_loading = true;
      				http( config.getUrl('logs_show'),{count : vm.page.size,offset : vm.page.offset})
      				.then( function( data ){
      					if( data && ( data.error_code || data.error_message)){
      						myUtils.tip({
	      						msg : data.error_message || config.error_tip
	      					},function(){
	      						vm.log_loading = false;
	      					})
      						return false;
      					}
      					if( data && data.data ){
      						vm.log_loading = false;
      						if( $.isArray(data.data.logs) && data.data.logs[0]){
      							angular.forEach( data.data.logs,function( vv ){
	      							if( vv.operation && vv.operation.indexOf('删除') > -1 ){
	      								vv.del = true;
	      							}
	      							var curTime = myUtils.createTime();
	      							var time = curTime - parseInt(vv.create_time)*1000;
	      							time = myUtils.counter( time,'day:hour:min');
	      							time = time.split(':');
	      							if( time[0] && parseInt(time[0])){
	      								if( parseInt(time[0]) < 2){
	      									vv.time = parseInt(time[0])+'天前'
	      								}else{
	      									vv.time = vv.cre_time;
	      								}
	      							}else if( time[1] && parseInt(time[1]) ){
	      								vv.time = parseInt(time[1])+'小时前';
	      							}else if( time[2] && parseInt(time[2]) ){
	      								vv.time = parseInt(time[2])+'分钟前'
	      							}else{
	      								vv.time = '刚刚';
	      							}
	      							switch(vv.moudle_id){
	      								case '文稿' :{
	      									vv.mark = 'wengao';
	      									vv.color  = '#68c986';
	      									break;
	      								}
	      								case '图集' :{
	      									vv.mark = 'tuji';
	      									vv.color  = '#a988e1';
	      									break;
	      								}
	      								case '视频' :{
	      									vv.mark = 'shipin';
	      									vv.color  = '#bca9a2';
	      									break;
	      								}
	      								case '外链' :{
	      									vv.mark = 'wailian';
	      									vv.color  = '#8dc9fb';
	      									break;
	      								}
	      							}
									vv.img = myUtils.createImgsrc(vv.avatar, {width: 80, height: 80})
	      						});
	      						vm.logList = data.data.logs;
	      						vm.page.len = data.data.total;
	      						vm.page.num = Math.ceil(data.data.total/vm.page.size);
      						}
      					}
      				})
      			}
      		});
      		vm.init();
      	}
})();